# -*- coding: utf-8 -*-
from imp import reload
import json
from flask import render_template, request, session,redirect,url_for
from app.main.sessions import load_sessions
from . import remark
from app.models import UserInfo13per
import datetime
import sys
reload(sys)
sys.setdefaultencoding('utf8')



@remark.route('/publish')
def publish():
    user_id = session.get('user_id')
    if not user_id:
        return redirect(url_for('main.get_login'))
    user = session.get('user_name')
    uid = request.args.get('uid')
    sessions = load_sessions()
    result = sessions.query(UserInfo13per).filter(UserInfo13per.studentId == uid).first()
    sessions.close()
    return render_template('publish.html', result=result,user=user)


@remark.route('/addremark')
def getremark():
    user_id = session.get('user_id')
    if not user_id:
        return redirect(url_for('main.get_login'))
    return render_template('addremark.html')



@remark.route('/publish_handle/', methods=['POST'], endpoint='publish_handle')
def publish_handle():
       user_id = session.get('user_id')
       if not user_id:
            return redirect(url_for('main.get_login'))
       user_name = session.get('user_name')
       data = request.get_data()
       if data == None:
            return 'Error'
       data = json.loads(data)
       type1 = data.get('type')
       text = data.get('text')
       time = datetime.datetime.now().strftime('%F %T')
       uid = data.get('uid')
       if type1== '1':
           sessions = load_sessions()  # 获取数据库会话
           result = sessions.query(UserInfo13per).get(uid)
           # 清空记录
           # if result:
           #     result.DutyThing = 'None'
           #     result.PalmThing = 'None'
           #     result.ScientificFruit= 'None'
           #     result.StrongSuit = 'None'
           #     result.Practice = 'None'
           #     result.OtherThing = 'None'
           #     sessions.commit()  #
           #     sessions.close()
           if result.DutyThing == None or result.DutyThing == 'None' or result.DutyThing == '无':
               result.DutyThing = ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
           else:
               result.DutyThing =  result.DutyThing +""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
       elif type1 == '2':
           sessions = load_sessions()  # 获取数据库会话
           result = sessions.query(UserInfo13per).get(uid)
           if result.PalmThing == None or result.PalmThing == 'None' or result.PalmThing == '无':
               result.PalmThing = ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
           else:
               result.PalmThing = "" + result.PalmThing + ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
       elif type1 == '3':
           sessions = load_sessions()  # 获取数据库会话
           result = sessions.query(UserInfo13per).get(uid)
           if result.ScientificFruit == None or result.ScientificFruit == 'None' or result.ScientificFruit == '无':
               result.ScientificFruit = ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
           else:
               result.ScientificFruit = "" + result.ScientificFruit+ ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
       elif type1 == '4':
           sessions = load_sessions()  # 获取数据库会话
           result = sessions.query(UserInfo13per).get(uid)
           if result.Practice == None or result.Practice == 'None' or result.Practice == '无':
               result.Practice = ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
           else:
               result.Practice = "" + result.Practice +""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
       elif type1 == '5':
           sessions = load_sessions()  # 获取数据库会话
           result = sessions.query(UserInfo13per).get(uid)
           if result.StrongSuit == None or result.StrongSuit == 'None' or result.StrongSuit == '无':
               result.StrongSuit = ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
           else:
               result.StrongSuit = "" + result.StrongSuit +""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
       else:
           sessions = load_sessions()  # 获取数据库会话
           result = sessions.query(UserInfo13per).get(uid)
           # 清空记录
           # if result:
           #     result.OtherThing = 'None'
           #     result.ScientificFruit= 'None'
           #     result.Practice = 'None'
           #     result.DutyThing = 'None'
           #     sessions.commit()  #
           #     sessions.close()
           if result.OtherThing == None or result.OtherThing == 'None' or result.OtherThing == '无' :
               result.OtherThing = ""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()
           else:
               result.OtherThing = "" + result.OtherThing +""+time+""+text+"—"+user_name+";"
               sessions.commit()
               sessions.close()

       return "success"